import 'dart:async';

import 'package:flutter/material.dart';
import 'package:memo/state/model/work_record.dart';
import 'package:memo/state/work_record_list_state.dart';
import 'package:provider/provider.dart';
import 'package:intl/intl.dart';

import 'package:flutter_easyrefresh/easy_refresh.dart';

import 'home_page.dart';

const divider = Divider(height: 1.0);

class WorkRecordListPage extends StatefulWidget {
  @override
  _WorkRecordListPageState createState() => _WorkRecordListPageState();
}

class _WorkRecordListPageState extends State<WorkRecordListPage> {
  GlobalKey<EasyRefreshState> _easyRefreshKey = GlobalKey<EasyRefreshState>();
  GlobalKey<RefreshHeaderState> _headerKey = GlobalKey<RefreshHeaderState>();
  GlobalKey<RefreshFooterState> _footerKey = GlobalKey<RefreshFooterState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("打卡记录")),
      body: Center(
        child: Consumer<WorkRecordListState>(
          builder: (context, state, _) {
            return state.isLoading ? LoadingWidget() : EasyRefresh(
              key: _easyRefreshKey,
              behavior: ScrollOverBehavior(),
              refreshHeader: _refreshHeader(),
              refreshFooter: _refreshFooter(),
              child: ListView.builder(
                  //ListView的Item
                  itemCount: state.len(),
                  itemBuilder: (BuildContext context, int index) {
                    final record = state.list[index];
                    return WorkRecordListTile(
                        key: Key(record.id), workRecord: record);
                  }),
              onRefresh: () async {
                await Future.delayed(const Duration(seconds: 1), () {
                  _easyRefreshKey.currentState.waitState(() {
                    state.loadMore = true;
                  });
                });
              },
              loadMore: state.loadMore
                  ? () async {
                      await new Future.delayed(const Duration(seconds: 1),
                          () async {
                        await state.loadMoreData();
                      });
                    }
                  : (){},
            );
          },
        ),
      ),
//      persistentFooterButtons: <Widget>[
//        FlatButton(
//            onPressed: () {
//              _easyRefreshKey.currentState.callRefresh();
//            },
//            child: Text("刷新", style: TextStyle(color: Colors.black))),
//        FlatButton(
//            onPressed: () {
//              _easyRefreshKey.currentState.callLoadMore();
//            },
//            child: Text("加载", style: TextStyle(color: Colors.black)))
//      ], // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  Widget _refreshHeader() {
    return ClassicsHeader(
      key: _headerKey,
      refreshText: "下拉刷新",
      refreshReadyText: "释放刷新",
      refreshingText: "正在刷新...",
      refreshedText: "刷新结束",
      moreInfo: "更新于 ${TimeOfDay.now().format(context)}",
      bgColor: Colors.transparent,
      textColor: Colors.black87,
      moreInfoColor: Colors.black54,
      showMore: true,
    );
  }

  Widget _refreshFooter() {
    return ClassicsFooter(
      key: _footerKey,
      loadText: "上拉加载",
      loadReadyText: "释放加载",
      loadingText: "正在加载",
      loadedText: "加载结束",
      noMoreText: "没有更多数据",
      moreInfo: "更新于 ${TimeOfDay.now().format(context)}",
      bgColor: Colors.transparent,
      textColor: Colors.black87,
      moreInfoColor: Colors.black54,
      showMore: true,
    );
  }
}

class WorkRecordListTile extends StatelessWidget {
  final WorkRecord workRecord;

  const WorkRecordListTile({Key key, this.workRecord}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    print("listTile: $workRecord");
    return ListTile(
      title: Text(DateFormat("yyyy-MM-dd").format(workRecord.time),
          style: TextStyle(fontWeight: FontWeight.bold)),
      trailing: SizedBox(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Icon(Icons.wb_sunny,
                color: workRecord.morningFlag ? Colors.yellow : Colors.grey),
            Icon(Icons.brightness_2,
                color: workRecord.nightFlag ? Colors.blue : Colors.grey),
          ],
        ),
        width: 60.0,
      ),
    );
  }
}
